package com.appkubes.mall.restful.application.cart.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.appkubes.mall.core.entity.MallCart;
import com.appkubes.mall.core.service.impl.BaseMallCartServiceImpl;
import com.appkubes.mall.restful.application.cart.dao.CartDao;
import com.appkubes.mall.restful.application.cart.entity.model.CartListModel;
import com.appkubes.mall.restful.application.cart.entity.vo.CartGetVO;
import com.appkubes.mall.restful.application.cart.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class CartServiceImpl extends BaseMallCartServiceImpl implements CartService {

    @Autowired
    CartDao cartDao;


    public MallCart getCartByIds(String userKey, String goodsKey, String storeKey, String attrkeys) {
        MallCart query = new MallCart();
        query.setUserKey(userKey);
        query.setGoodsKey(goodsKey);
        query.setStoreKey(storeKey);
        query.setAttrKeys(attrkeys);
        List<MallCart> result = this.selectListByEntity(query);
        return null != result && result.size() > 0 ? result.get(0) : null;
    }

    public Integer getCartNums(String cartKey) {
        return cartDao.getCartNums(cartKey);
    }

    public List<CartListModel> getCarts(CartGetVO cartGetVO) {
        List<CartListModel> cartList = cartDao.getCarts(cartGetVO.getUserKey());
        if (null != cartList && cartList.size() > 0) {
            for (CartListModel m : cartList) {
                if (null != m.getPics()) {
                    if (StringUtils.isEmpty(m.getPics())) {
                        m.setPic(JSONObject.parseArray(m.getPics(), String.class).get(0));
                    }
                }
            }
        }
        return cartList;
    }


}
